package com.cdhong.shop.backend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdhong.shop.backend.entity.Goods;
import com.cdhong.shop.backend.entity.GoodsAlbum;
import com.cdhong.shop.backend.entity.ResponseEntity;
import com.cdhong.shop.backend.service.IGoodsAlbumService;
import com.cdhong.shop.backend.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author CDHong
 * @since 2024-04-17
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsAlbumService goodsAlbumService;

    /**
     * 查询商品列表
     *
     * @return 返回商品列表
     */
    @GetMapping("/list")
    public ResponseEntity list(@RequestParam("page") Integer pageIndex,
                               @RequestParam("pagesize") Integer pageSize) {
        Page<Goods> page = goodsService.page(new Page<>(pageIndex, pageSize));
        if (page.getRecords().isEmpty()) return ResponseEntity.error("暂无商品列表！！");
        return ResponseEntity.page(page.getTotal(), page.getRecords());
    }


    /**
     * 查询单个商品
     *
     * @param id 商品ID
     * @return 返回当前ID对应的商品信息
     */
    @GetMapping("one")
    public ResponseEntity one(Integer id) {
        Goods goods = goodsService.getById(id);
        if (Objects.nonNull(goods)) {
            // 查询当前商品下的所有图册
            List<String> albumPicList = goodsAlbumService.lambdaQuery()
                    .eq(GoodsAlbum::getGoodsId, goods.getId())
                    .select(GoodsAlbum::getPicture).list()
                    .stream()
                    .map(GoodsAlbum::getPicture).collect(Collectors.toList());
            goods.setAlbum(albumPicList);
            return ResponseEntity.data(goods);
        }
        return ResponseEntity.error("该商品没有数据，请检查ID");
    }


    /**
     * 添加商品
     *
     * @param goods 商品数据对象
     * @return 返回是否添加商品成功
     */
    @PostMapping("add")
    public ResponseEntity add(@RequestBody Goods goods) {
        // 保存商品信息
        boolean flg = goodsService.save(goods);
        if (flg) { // 保存图册信息
            List<GoodsAlbum> goodsAlbumList = getAlbums(goods);
            boolean flg2 = goodsAlbumService.saveBatch(goodsAlbumList);
            if (flg2) {
                return ResponseEntity.ok("添加商品成功！！");
            }
        }
        return ResponseEntity.error("添加商品失败");
    }


    /**
     * 编辑商品
     *
     * @param goods 商品数据对象
     * @return 返回是否编辑商品成功
     */
    @PostMapping("edit")
    public ResponseEntity edit(@RequestBody Goods goods) {
        boolean flg = goodsService.updateById(goods);
        if (flg) {//更新商品图册
            // 删除当前商品的所有图册，再进行新增
            goodsAlbumService.lambdaUpdate().eq(GoodsAlbum::getGoodsId, goods.getId()).remove();
            List<GoodsAlbum> goodsAlbumList = getAlbums(goods);
            boolean saveBatchFlg = goodsAlbumService.saveBatch(goodsAlbumList);
            if (saveBatchFlg) {
                return ResponseEntity.ok("更新商品成功！！");
            }
        }
        return ResponseEntity.error("更新商品失败");
    }

    /**
     * 删除商品
     *
     * @param map 商品ID
     * @return 返回是否删除商品成功
     */
    @PostMapping("del")
    public ResponseEntity del(@RequestBody Map<String,Integer> map) {
        Integer goodsId = map.get("id");
        boolean flg = goodsService.removeById(goodsId);
        goodsAlbumService.lambdaUpdate().eq(GoodsAlbum::getGoodsId, goodsId).remove();
        if (flg) return ResponseEntity.ok("删除成功！！");
        return ResponseEntity.error("删除失败！！");
    }

    /**
     * @Description 根据商品ID和商品图册进行对象组装
     * @Author CDHong
     * @Param [goods]
     * @Date 2024/4/21 14:50
     **/
    private List<GoodsAlbum> getAlbums(Goods goods) {
        List<GoodsAlbum> goodsAlbumList = new ArrayList<>();
        List<String> album = goods.getAlbum();
        for (String imgPath : album) {
            GoodsAlbum goodsAlbum = GoodsAlbum.builder().goodsId(goods.getId()).picture(imgPath).build();
            goodsAlbumList.add(goodsAlbum);
        }
        return goodsAlbumList;
    }
}
